<?php
require("includes/constantesMvial.php");

function firstpicture($ad_id){
	$queryf = "SELECT * FROM mvial_picture WHERE pic_ad_id = " . $ad_id . " and main = 1";
	$sqlf=mysql_query($queryf);
	while($rgf=mysql_fetch_object($sqlf)) {
		//$imagen = "/timthumb.php?src=/images/" . str_replace("_tmb2", "", $rgf->filename) . "&w=165&h=175&cz=1";
		$imagen = "/images/" . str_replace("_tmb2", "", $rgf->filename);
	}
	return $imagen;
}


function listardetacados($categoria,$cantidad){
	$contar_rows = 0;
	if($categoria == 0){
	$queryp = "select * from mvial_ad where ad_cat_id in (SELECT cat_id FROM mvial_category WHERE cat_name like '%repuesto%') and ad_id in (select pic_ad_id from mvial_picture where main = 1)";
	} else {
	$queryp = "select * from mvial_ad where ad_cat_id in (SELECT cat_id FROM mvial_category WHERE cat_fatherid = " . $categoria . ")  and ad_id in (select pic_ad_id from mvial_picture where main = 1)";
	}
   
	// $queryp .= " and ad_is_special = 1";
	$queryp .= " and ad_is_premium = 1";
	$queryp .= " and ad_enabled = 1";
	$query .= " and ad_is_validated = 1";
	//$queryp .= " AND ad_id < 1600";
	$queryp .= " order by rand() limit " . $cantidad;
    // logMsg('qry:', $queryp);
	//echo($queryp);
	$sqlp=mysql_query($queryp);
	$resultado = '<table cellpadding="0" cellspacing="0" border="0" width="510" align="center">';
	while($rgp=mysql_fetch_object($sqlp)) {
	//$link_articulo = "/detail.php?id=" . $rgp->ad_id;
	$link_articulo = linkDetail($rgp->ad_id);
	$contar_rows = $contar_rows + 1;
		if($contar_rows == 1)	$resultado .= '<tr>';
		$resultado .= '<td align="center" class="tddestacados" valign="top"><a href="'.$link_articulo.'"><img src="' . firstpicture($rgp->ad_id) . '" width="165" border=0 class="img_destacado" title="'.$rgp->ad_title.'"></a><br>' .$rgp->ad_title.'<br>'.$rgp->ad_moneda.' ' . $rgp->ad_precio.'</td>';
		if($contar_rows == 2){
			$contar_rows = 0;
			$resultado .= '</tr><td height="10" colspan="5"></td>';
		} else {
			$resultado .= '<td width="10"></td>';
		}
	}
	//if($contar_rows == 1)	$resultado .= '<td align="center" class="tddestacados" valign="top"></td><td width="10"></td><td align="center" class="tddestacados" valign="top"></td>';
	if($contar_rows == 1)	$resultado .= '<td align="center" class="tddestacados" valign="top"></td>';
	$resultado .= '</table>';
	return $resultado;
}



$tplDet=new TplLoad;
$tplDet->display("destacados.tpl");
?>
